package com.b1502.store2.adapter;


import android.content.Context;


import android.graphics.Color;

import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;


import android.view.View;
import android.view.ViewGroup;


import android.widget.Button;

import android.widget.CheckBox;

import android.widget.CompoundButton;
import android.widget.ImageView;



import android.widget.RelativeLayout;
import android.widget.TextView;


import com.b1502.store2.R;

import com.b1502.store2.StoreApp;
import com.b1502.store2.model.bean.CartBean;


import com.b1502.store2.util.ImageLoaderUtils;

import com.b1502.store2.util.UrlUtil;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 类描述:
 * 作者：陈文梦
 * 时间:2017/2/28 10:12
 * 邮箱:18310832074@163.com
 */

public
class
CartAdapter extends RecyclerView.Adapter implements View.OnClickListener {

    private Context context;
    private CartBean[] cartBeen;
    private DisplayImageOptions options;
    private OnClikerListener onClikerListener;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public void setOnClikerListener(OnClikerListener onClikerListener) {

        this.onClikerListener = onClikerListener;

    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public CartAdapter(CartBean[] cartBeen, Context context) {
        this.cartBeen = cartBeen;
        this.context = context;
        options = ImageLoaderUtils.initOptions();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = View.inflate(context, R.layout.cart_item, null);
        MyCartViewHolder myCartViewHolder = new MyCartViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return myCartViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        MyCartViewHolder myCartViewHolder = (MyCartViewHolder) holder;
        ImageLoader.getInstance().displayImage(UrlUtil.getImageUrl(cartBeen[position].getImageUrl()), myCartViewHolder.imageView, options);
        myCartViewHolder.textView1.setText(cartBeen[position].getName());
        myCartViewHolder.textView2.setText("￥" + cartBeen[position].getPrice() + "");
        //判断编辑按钮决定是否隐藏
        if(StoreApp.flag){
            myCartViewHolder.relativeLayout.setVisibility(View.VISIBLE);
            myCartViewHolder.deleteimage.setVisibility(View.VISIBLE);
        }else {
            myCartViewHolder.relativeLayout.setVisibility(View.GONE);
            myCartViewHolder.deleteimage.setVisibility(View.GONE);
        }
        //判断每个商品的数目最少是否小于1
        if(cartBeen[position].getCount()==1){

            myCartViewHolder.jianbtn.setBackgroundColor(Color.parseColor("#888888"));

        }else {

            myCartViewHolder.jianbtn.setBackgroundResource(R.drawable.bacg);
        }
        myCartViewHolder.itemView.setTag(position);
        myCartViewHolder.checkBox.setChecked(cartBeen[position].isCheck());
        myCartViewHolder.numbtn.setText(cartBeen[position].getCount()+"");
        //复选框监听
        myCartViewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (onClikerListener != null) {

                    onClikerListener.checkBoxListener(position, isChecked);

                }

            }
        });
        //删除按钮监听
        myCartViewHolder.deleteimage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if(onClikerListener!=null){

                onClikerListener.deleteListener(position);
                }

            }
        });

        //减少购买的商品
        myCartViewHolder.jianbtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(onClikerListener!=null){

                onClikerListener.jianListener(position);
                }
            }
        });
        //增加购买的商品
        myCartViewHolder.jiabtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(onClikerListener!=null){

                onClikerListener.jiaListener(position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return cartBeen.length;
    }

    @Override
    public void onClick(View v) {

        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }

    }

    class MyCartViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView, deleteimage;
        CheckBox checkBox;
        TextView textView1, textView2;
        Button jianbtn, numbtn, jiabtn;
        RelativeLayout relativeLayout;
        public MyCartViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.shop_image);
            deleteimage = (ImageView) itemView.findViewById(R.id.shanchu);
            checkBox = (CheckBox) itemView.findViewById(R.id.shop_check);
            textView1 = (TextView) itemView.findViewById(R.id.shop_name);
            textView2 = (TextView) itemView.findViewById(R.id.shop_yhjia);
            jianbtn = (Button) itemView.findViewById(R.id.jian1);
            numbtn = (Button) itemView.findViewById(R.id.jiashop);
            jiabtn = (Button) itemView.findViewById(R.id.jia1);
            relativeLayout= (RelativeLayout) itemView.findViewById(R.id.r12);
        }
    }

    public interface OnClikerListener {

        void checkBoxListener(int position, boolean isChecked);

        void deleteListener(int position);

        void jianListener(int position);

        void jiaListener(int position);
    }

    public static interface OnRecyclerViewItemClickListener {

        void onItemClick(View view, int position);
    }
}
